package com.w7vm.menuweb.utility;

import java.util.Iterator;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;

import org.primefaces.component.picklist.PickList;
import org.primefaces.model.DualListModel;

import com.w7vm.menuweb.entity.AcessoWeb;


@FacesConverter(value = "perfilAdmPickListConverter") 

public class PerfilAdmContatPickListConverter implements Converter { 

	public Object getAsObject(FacesContext arg0, UIComponent arg1, String arg2) { 
		Object ret = null; 
		
		if (arg1 instanceof PickList) { 
			Object dualList = ((PickList) arg1).getValue(); 
			DualListModel dl = (DualListModel) dualList; 
			
			for (Iterator iterator = dl.getSource().iterator(); iterator .hasNext();) 
			{ 
				Object o = iterator.next(); 
				String id = (new StringBuilder()).append( ((AcessoWeb) o).getIdAcessoWeb()).toString(); 
				if (arg2.equals(id)) { ret = o; break; } } if (ret == null) { 
					for (Iterator iterator1 = dl.getTarget().iterator(); iterator1 .hasNext();) { 
						Object o = iterator1.next(); 
						String id = (new StringBuilder()).append( ((AcessoWeb) o).getIdAcessoWeb()).toString(); 
						if (arg2.equals(id)) { 
							ret = o; break; 
							} 
						} 
					} 
				} 
		return ret; 
	} 
	
	
	@Override
	public String getAsString(FacesContext context, UIComponent component, Object value) {
		int retorno = 0;
		if (!(value == null)) {
			AcessoWeb filial = new AcessoWeb();
			filial = (AcessoWeb) value;
			retorno = filial.getIdAcessoWeb();
		}
		return Integer.toString(retorno);
	}

}
